package com.data.center.service.impl;

import com.data.center.entity.OrganizationTag;
import com.data.center.exception.exception.BizException;
import com.data.center.mapper.OrganizationTagMapper;
import com.data.center.service.OrganizationTagService;
import com.data.center.util.ForeignManage.ForeignDataUtil;
import com.data.center.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

/**
 * @author Li_Jx
 * @description:
 * @date 2023/11/8 17:47
 */
@Service
public class OrganizationTagServiceImpl implements OrganizationTagService {
    @Autowired
    private OrganizationTagMapper organizationTagMapper;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private ForeignDataUtil foreignDataUtil;


    @Override
    public List<OrganizationTag> select(OrganizationTag organizationTagFilter) {
        List<OrganizationTag> organizationTagList = organizationTagMapper.select(organizationTagFilter);
        return organizationTagList;
    }

    @Override
    public void update(OrganizationTag organizationTag) {
        //TODO 修改者此处在后期需修改
        organizationTag.setName(null);
        organizationTag.setCreator(null);
        organizationTag.setCreateTime(null);
        organizationTag.setModifier("");
        organizationTag.setLastModifiedTime(new Date());
        organizationTagMapper.update(organizationTag);
    }

    @Override
    public List<OrganizationTag> findOrgTag(String name) {
        List<OrganizationTag> organizationTagList = organizationTagMapper.findOrgTag(name);
        return organizationTagList;
    }

    @Override
    public void addOrgTag(OrganizationTag organizationTag) {
        organizationTag.setId(String.valueOf(snowflakeIdWorker.nextId()));
        //TODO 创建者和修改者此处在后期需修改
        organizationTag.setCreator("");
        organizationTag.setCreateTime(new Date());
        organizationTag.setModifier("");
        organizationTag.setLastModifiedTime(new Date());
        organizationTagMapper.insert(organizationTag);
    }
    @Override
    public void renameOrgTag(OrganizationTag organizationTag){
        //TODO 修改者此处在后期需修改
        organizationTag.setModifier("");
        organizationTag.setLastModifiedTime(new Date());
        organizationTagMapper.update(organizationTag);
    }

    @Override
    public void deleteByName(String name) {
        // 检查是否有外键关联
        try {
            if ( foreignDataUtil.searchByForeignKey(findOrgTag(name).get(0))) {
                organizationTagMapper.deleteByName(name);
            }
            else {
                throw new BizException("该标签名称存在外键关联，无法删除");
            }
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void mergeOrgTag(OrganizationTag sourceOrgTag, OrganizationTag targetOrgTag) {
        // 检查是否有外键关联
        try {
            foreignDataUtil.mergeByForeignKey(sourceOrgTag,targetOrgTag);
        } catch (InvocationTargetException e) {
            throw new BizException(e.getTargetException().getMessage());
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        organizationTagMapper.deleteByName(organizationTagMapper.select(targetOrgTag).get(0).getName());
    }
}
